package com.gibs.peixunapi.service.impl;

import com.gibs.peixunapi.dao.ExamClassInfoDao;
import com.gibs.peixunapi.dao.ExamInfoDao;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.model.ExamClassInfo;
import com.gibs.peixunapi.model.ExamInfo;
import com.gibs.peixunapi.model.TestPaper;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.service.ExamClassInfoService;
import com.gibs.peixunapi.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Transactional(rollbackFor = Exception.class)
@Service
public class ExamClassInfoServiceImpl implements ExamClassInfoService {

    @Autowired
    private ExamClassInfoDao examClassInfoDao;
    @Autowired
    private ExamInfoDao examInfoDao;


    @Override
    public Result create(@Valid ExamClassInfo object) {
        try {

//			objectDao.save(object);
            return ResultUtil.success(ResultEnum.CREATE_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Transactional
    @Override
    public Result save(@Valid ExamClassInfo object) {
        try {
//
//			ExamClassInfo examClassInfo = objectDao.findOne(object.getId());
//			ConverterUtil.copyNotNullProperties(object,examClassInfo);
//			objectDao.save(examClassInfo);
            return ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Override
    public Result delete(Integer id) {
        try {
//			String[] ids = id.split(",");
//			for (String str : ids) {
//				objectDao.delete(Integer.valueOf(str));
//			}
            return ResultUtil.success(ResultEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }


    @Override
    public Result get(Integer id) {
        try {
//			HashMap<String, Object> data = new HashMap<String, Object>();
//			ExamClassInfo object = objectDao.findOne(id);
//			if (object == null) {
//				Result(false, Status.FAILD, "查找失败", null);
//			}
//			HashMap<String,Object> map = new HashMap();
//			map.put("id", object.getId());
//			map.put("name", object.getName()==null?"":object.getName());
//			map.put("number", object.getNumber()==null?"":object.getNumber());
//			HashMap<String,Object> examInfoId = new HashMap<String,Object>();
//			examInfoId.put("name", object.getExamInfoId()==null?"":object.getExamInfoId().getName());
//			examInfoId.put("id", object.getExamInfoId()==null?"":object.getExamInfoId().getId());
//			map.put("examInfoId", examInfoId);
//			map.put("code", object.getCode()==null?"":object.getCode());
//			map.put("proportion", object.getProportion()==null?"":object.getProportion());
//			map.put("examStartTime", object.getExamStartTime()==null?"":DateUtils.toString(object.getExamStartTime()));
//			map.put("examEndTime", object.getExamEndTime()==null?"":DateUtils.toString(object.getExamEndTime()));
//			map.put("correctStartTime", object.getCorrectStartTime()==null?"":DateUtils.toString(object.getCorrectStartTime()));
//			map.put("correctEndTime ", object.getCorrectEndTime ()==null?"":DateUtils.toString(object.getCorrectEndTime ()));
//			map.put("announceResultTime", object.getAnnounceResultTime()==null?"":DateUtils.toString(object.getAnnounceResultTime()));
//			map.put("examStatus", object.getExamStatus()==null?"":object.getExamStatus());
//			data.put("object",map);

            return ResultUtil.success(ResultEnum.FIND_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Override
    public Result getList(String key) {
        try {
//			List<ExamClassInfo> list = new ArrayList<>();
//			if (!StringUtils.isEmpty(key)) {
//				list = objectDao.findLike("%" + key + "%");
//			} else {
//				list = objectDao.findAll();
//			}
//			if (list.size() == 0) {
//				PageResult(true, Status.OK, "无数据", new ArrayList(), 0L,0,0);
//			}
//			List<Object> data = new ArrayList<>();
//			for(ExamClassInfo object : list){
//				HashMap<String,Object> map = new HashMap();
//				map.put("id", object.getId());
//				map.put("name", object.getName()==null?"":object.getName());
//				map.put("number", object.getNumber()==null?"":object.getNumber());
//				HashMap<String,Object> examInfoId = new HashMap<String,Object>();
//				examInfoId.put("name", object.getExamInfoId()==null?"":object.getExamInfoId().getName());
//				examInfoId.put("id", object.getExamInfoId()==null?"":object.getExamInfoId().getId());
//				map.put("examInfoId", examInfoId);
//				map.put("code", object.getCode()==null?"":object.getCode());
//				map.put("proportion", object.getProportion()==null?"":object.getProportion());
//				map.put("examStartTime", object.getExamStartTime()==null?"":DateUtils.toString(object.getExamStartTime()));
//				map.put("examEndTime", object.getExamEndTime()==null?"":DateUtils.toString(object.getExamEndTime()));
//				map.put("correctStartTime", object.getCorrectStartTime()==null?"":DateUtils.toString(object.getCorrectStartTime()));
//				map.put("correctEndTime ", object.getCorrectEndTime ()==null?"":DateUtils.toString(object.getCorrectEndTime ()));
//				map.put("announceResultTime", object.getAnnounceResultTime()==null?"":DateUtils.toString(object.getAnnounceResultTime()));
//				map.put("examStatus", object.getExamStatus()==null?"":object.getExamStatus());
//				data.add(map);
//			}
            return ResultUtil.success(ResultEnum.FIND_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Override
    public Result getListPage(String key, Integer current, Integer limit) {
        try {
//			List<ExamClassInfo> list = new ArrayList<>();
//			limit = limit == null ? 10 : limit;
//			current = current == null ? 1 : current;
//			int offset = (current - 1) * limit;
//			Long total = null;
//			if (!StringUtils.isEmpty(key)) {
//				list = objectDao.findPageLike("%" + key + "%", offset, limit);
//				total = objectDao.countLike("%" + key + "%");
//			} else {
//				list = objectDao.findPage(offset, limit);
//				total = objectDao.count();
//			}
//			if (total == 0) {
//				PageResult(true, Status.OK, "无数据", new ArrayList(), 0L,0,0);
//			}
//			List<Object> data = new ArrayList<>();
//			for(ExamClassInfo object : list){
//				HashMap<String,Object> map = new HashMap();
//				map.put("id", object.getId());
//				map.put("name", object.getName()==null?"":object.getName());
//				map.put("number", object.getNumber()==null?"":object.getNumber());
//				HashMap<String,Object> examInfoId = new HashMap<String,Object>();
//				examInfoId.put("name", object.getExamInfoId()==null?"":object.getExamInfoId().getName());
//				examInfoId.put("id", object.getExamInfoId()==null?"":object.getExamInfoId().getId());
//				map.put("examInfoId", examInfoId);
//				map.put("code", object.getCode()==null?"":object.getCode());
//				map.put("proportion", object.getProportion()==null?"":object.getProportion());
//				map.put("examStartTime", object.getExamStartTime()==null?"":DateUtils.toString(object.getExamStartTime()));
//				map.put("examEndTime", object.getExamEndTime()==null?"":DateUtils.toString(object.getExamEndTime()));
//				map.put("correctStartTime", object.getCorrectStartTime()==null?"":DateUtils.toString(object.getCorrectStartTime()));
//				map.put("correctEndTime ", object.getCorrectEndTime ()==null?"":DateUtils.toString(object.getCorrectEndTime ()));
//				map.put("announceResultTime", object.getAnnounceResultTime()==null?"":DateUtils.toString(object.getAnnounceResultTime()));
//				map.put("examStatus", object.getExamStatus()==null?"":object.getExamStatus());
//				data.add(map);
//			}
//			PageData pageData = new PageData(total, limit, current);
            return ResultUtil.successPage(ResultEnum.SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }


    @Override
    public Result getOptions(String key) {
        try {
//			List<ExamClassInfo> list = new ArrayList<>();
//			if (!StringUtils.isEmpty(key)) {
//				list = objectDao.findLike("%" + key + "%");
//			} else {
//				list = objectDao.findAll();
//			}
//			List<Object> data = new ArrayList<>();
//			for (ExamClassInfo object : list) {
//				HashMap<String, Object> map = new HashMap();
//				map.put("value", object.getId());
//				map.put("name", object.getName());
//				data.add(map);
//			}
            return ResultUtil.success(ResultEnum.SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Override
    public Boolean isTestPaperUsed(Integer testPaperId) {
        //考试表中是否存在使用此试卷的考试
        List<ExamInfo> examInfoList = examInfoDao.findExamInfosByTestPaper_Id(testPaperId);
        if (examInfoList.isEmpty()) {
            return false;
        }
        //考试班级中是否有已经开始考试的
        List<Integer> idList = examClassInfoDao.isExamClassStart(examInfoList).stream()
                .map(e -> Integer.parseInt(e.toString()))
                .collect(Collectors.toList());
        return !idList.isEmpty();
    }
}

